//
//  HWWuKongCell.swift
//  HWanGame
//
//  Created by Apple on 2024/8/5.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWWuKongCateView: UITableViewHeaderFooterView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
         
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var sourceData: [HWWuKongCateModel] = [
        HWWuKongCateModel(title: "热聊", type: 0, isSel: true),
        HWWuKongCateModel(title: "评论", type: 1, isSel: false),
        HWWuKongCateModel(title: "攻略", type: 2, isSel: false),
        HWWuKongCateModel(title: "论坛", type: 3, isSel: false),
    ]
    
    var updateCateData: ((_ cateID: Int) -> Void)?
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        addSubview(collectionView)
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.scrollDirection = .horizontal
//        layout.itemSize = CGSize(width: 45*widthScale, height: 38*widthScale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongCateCell.self, forCellWithReuseIdentifier: HWWuKongCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        sourceData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongCateCell.description(), for: indexPath) as? HWWuKongCateCell else { return UICollectionViewCell() }
        cell.model = sourceData[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let type = sourceData[indexPath.row].type
        if type == 0 || type == 1 {
            var models = [HWWuKongCateModel]()
            sourceData.forEach { model in
                var itemModel = model
                itemModel.isSel = false
                models.append(itemModel)
            }
            models[indexPath.row].isSel = true
            sourceData = models
            collectionView.reloadData()
        }        
        updateCateData?(type)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = sourceData[indexPath.row].title
        let font = 14.font_medium
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])

        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width
        
        return CGSize(width: width, height: 38.0*widthScale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}

//MARK: ==========================UICollectionViewCell==========================

//MARK: 图文分类cell
class HWHomeCateCell: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(14.font_medium).alignment(.center).end()
    
    var model: HWNewHomeGameModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.name
            iconImageView.sd_setImage(with: URL(string: md.icon))
        }
    }
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
            nameLabel
        }
        
        iconImageView.top(0).size(40*widthScale).centerHorizontally()
        nameLabel.leading(0).trailing(0).centerHorizontally()
        nameLabel.Top == iconImageView.Bottom + 5*widthScale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 下划线分类cell
class HWWuKongCateCell: UICollectionViewCell {
    
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(14.font_medium).alignment(.center).text("热聊").end()
    private let lineView = c_make_ex(UIView.self)
        .bgColor(.black).corner(0.7).end()
    
    var model: HWWuKongCateModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.title
            lineView.isHidden = !md.isSel
        }
    }
    
    var gameModel: HWHomeNormalGameTypeModel?{
        didSet{
            guard let md = gameModel else { return }
            nameLabel.text = md.name
            lineView.isHidden = !md.isSel
            if md.isSel {
                nameLabel.textColor = "#2E3033".toColor
            }else{
                nameLabel.textColor = "#47494D".toColor
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            nameLabel
            lineView
        }
        
        
        nameLabel.top(8*widthScale).centerHorizontally()
        lineView.height(2*widthScale).bottom(5*widthScale)
        lineView.Width == nameLabel.Width
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 游戏通用cell
class HWWuKongNormalGameCell: UICollectionViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .corner(12*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(14.font_semiBold).end()
    private let tagLabel = c_make_ex(UILabel.self)
        .bgColor("#E55C67".toColor.alpha(0.1)).font(8.font_medium)
        .corner(4*widthScale).color("#E55C67").end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(13.font_semiBold).end()
    private let scoreLabel = UIIconButton(style: .leading(itemSpace: 2))
    
    var model: HWHomeInfoDetailGameModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageV.sd_setImage(with: URL(string: md.icon))
            nameLabel.text = md.name
            subLabel.text = md.system_type_text
            scoreLabel.setTitle("\(md.point)", for: .normal)
        }
    }
    
    var tuijianMd: HWNewHomeGameModel? {
        didSet{
            guard let md = tuijianMd else { return }
            
            iconImageV.sd_setImage(with: URL(string: md.icon))
            nameLabel.text = md.name
            let tagStr = md.biaoqian.joined(separator: " · ")
            subLabel.text = tagStr
            scoreLabel.setTitle("\(md.point)", for: .normal)
            if md.pay.count > 0 {
                tagLabel.isHidden = false
                tagLabel.text = " " + md.pay + " "
            }else{
                tagLabel.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            nameLabel
            tagLabel
            subLabel
            scoreLabel
        }
             
        scoreLabel.style { v in
            v.iconSize = CGSize(width: 14*widthScale, height: 14*widthScale)
            v.setImage("ScoreStar".toImage, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .normal)
            v.titleLabel?.font = 12.font_medium
            v.isUserInteractionEnabled = false
        }
        
        iconImageV.leading(12*widthScale).size(60*widthScale).centerVertically()
        nameLabel.leading(84*widthScale).top(15*widthScale).width(<=140)
        tagLabel.height(15*widthScale)
        tagLabel.Leading == nameLabel.Trailing + 3*widthScale
        tagLabel.CenterY == nameLabel.CenterY
        subLabel.leading(84*widthScale).trailing(20*widthScale)
        subLabel.Top == nameLabel.Bottom + 3*widthScale
        scoreLabel.leading(84*widthScale)
        scoreLabel.Bottom == iconImageV.Bottom + 5*widthScale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 装备资料cell
class HWWuKongSlideCateCell: UICollectionViewCell {
    
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_regular).alignment(.center)
        .bgColor("#D7D8DB").corner(4*widthScale).end()
    var model: HWStrategyBlockItemModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = md.title
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            nameLabel
        }
                
        nameLabel.fillContainer()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 主线图⽂攻略cell
class HWWuKongImageTextStrategyCell: UICollectionViewCell {
    
    private let backView = c_make_ex(UIView.self)
        .bgColor("#D7D8DB").corner(12*widthScale).end()
    private let iconImageV = c_make_ex(UIImageView.self)
        .corner(8*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(13.font_regular).alignment(.center)
        .text("主线图文攻略主线图文攻略").end()
    
    var model: HWStrategyBlockItemModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.sd_setImage(with: URL(string: md.image))
            nameLabel.text = md.title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                iconImageV
                nameLabel
            }
        }
                
        backView.fillContainer()
        iconImageV.leading(8*widthScale).trailing(8*widthScale).top(8*widthScale).height(130*widthScale)
        nameLabel.leading(8*widthScale).trailing(8*widthScale)
        nameLabel.Top == iconImageV.Bottom + 8*widthScale
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


//MARK: 主线图⽂攻略cell
class HWWuKongGameStrategyCell: UICollectionViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .bgColor(.orange).corner(8*widthScale).mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(12.font_semiBold)
        .text("图文版本剧情详解").end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(11.font_semiBold)
        .text("20分钟前 · 112K浏览").end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            titleLabel
            contentLabel
        }
                
        iconImageV.leading(0).trailing(0).top(0).height(110*widthScale)
        titleLabel.leading(4*widthScale).trailing(4*widthScale)
        titleLabel.Top == iconImageV.Bottom + 8*widthScale
        contentLabel.leading(4*widthScale).trailing(4*widthScale)
        contentLabel.Top == titleLabel.Bottom
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
